set -e
set -o pipefail

if [ -z "${LOCATION}" ]; then LOCATION=""; fi

if [ -z "${TEMPERATURE}" ]; then TEMPERATURE=301.15; fi
if [ -z "${MIN_PEAK_FORCE_PN}" ]; then MIN_PEAK_FORCE_PN="(50.1,40),(4360,65)"; fi
if [ -z "${MIN_PEAK_FORCE_RELATIVE_TO_EMPIRICAL_BASELINE_PN}" ]; then MIN_PEAK_FORCE_RELATIVE_TO_EMPIRICAL_BASELINE_PN=0; fi
if [ -z "${MEDIAN_ABSOLUTE_DEVIATION_LOWER_BOUND_PN}" ]; then MEDIAN_ABSOLUTE_DEVIATION_LOWER_BOUND_PN="(50.1,5),(4360,22)"; fi
if [ -z "${MIN_PEAK_CONTOUR_LENGTH}" ]; then MIN_PEAK_CONTOUR_LENGTH=10; fi
if [ -z "${MIN_CONTOUR_LENGTH_DELTA_NM}" ]; then MIN_CONTOUR_LENGTH_DELTA_NM=5; fi
if [ -z "${LEFT_MOSTLY_HIGHER_WINDOW_SIZE_NM}" ]; then LEFT_MOSTLY_HIGHER_WINDOW_SIZE_NM=6.1; fi
if [ -z "${LEFT_MOSTLY_HIGHER_FRACTION}" ]; then LEFT_MOSTLY_HIGHER_FRACTION=0.7; fi
if [ -z "${LEFT_MOSTLY_HIGHER_PN}" ]; then LEFT_MOSTLY_HIGHER_PN=1.0; fi
if [ -z "${RIGHT_MUCH_HIGHER_THAN_ALL_PN}" ]; then RIGHT_MUCH_HIGHER_THAN_ALL_PN="(50.1,5),(4360,5)"; fi
if [ -z "${RIGHT_MUCH_HIGHER_PN}" ]; then RIGHT_MUCH_HIGHER_PN="(50.1,23),(4360,60)"; fi
if [ -z "${AT_MOST_ONE_PEAK_WINDOW_NM}" ]; then AT_MOST_ONE_PEAK_WINDOW_NM=5; fi
if [ -z "${SLOPE}" ]; then SLOPE=""; fi
if [ -z "${SVG_DEF_SCALE}" ]; then SVG_DEF_SCALE=0.125; fi
if [ -z "${CL_START_NM}" ]; then CL_START_NM=15.0; fi
if [ -z "${CL_END_NM}" ]; then CL_END_NM=23.0; fi

if [ -z "${NANO_DRIVER_DELAY_MILLIS}" ]; then NANO_DRIVER_DELAY_MILLIS=0; fi
if [ -z "${NANO_DRIVER_SLEEPER}" ]; then NANO_DRIVER_SLEEPER="system"; fi

if [ -z "${FETCH_NM}" ]; then FETCH_NM=225; fi
if [ -z "${FETCH_SURFACE_DELAY_MS}" ]; then FETCH_SURFACE_DELAY_MS=17; fi
if [ -z "${REFOLDING_NM}" ]; then REFOLDING_NM=160; fi
if [ -z "${HOVER_MILLIS}" ]; then HOVER_MILLIS=50; fi
if [ -z "${BREAK_NM}" ]; then BREAK_NM=435; fi
if [ -z "${REFOLDING_APPROACH_VELOCITY_NM_PER_S}" ]; then REFOLDING_APPROACH_VELOCITY_NM_PER_S=2180; fi
if [ -z "${VELOCITY_NM_PER_S}" ]; then VELOCITY_NM_PER_S=2180; fi
if [ -z "${DEBUG_PATTERN}" ]; then DEBUG_PATTERN="glob:"; fi

if [ -z "${PUSH_NM}" ]; then PUSH_NM=10.0; fi
if [ -z "${ALLOWED_TIME_RELATIVE_ERROR}" ]; then ALLOWED_TIME_RELATIVE_ERROR="0.06,0.8,0.18,0.8,0.04,0.8,0.04"; fi

# BATCH_CLASSPATH for debugging: mvn dependency:build-classpath plus target/classes
if [ -z "${BATCH_CLASSPATH}" ]; then BATCH_CLASSPATH="target/refolding-core-0.1-jar-with-dependencies.jar"; fi

for d in $DIRS; do p=$(dirname ${d}); b=$(basename ${d}); s=${d/\/home\/$(whoami)\/work\/curves/\/mnt\/${LOCATION}\/capture}; (cd ~/work/rcs/refolding-read-only/core && nice -n 10 java -ea -cp ${BATCH_CLASSPATH} -Djava.awt.headless=true -Djava.util.logging.config.file=src/main/other/fileAndConsole.properties it.unibo.refolding.pico.stripchart.FetchRefoldBreak --nanoDriverHost=${LOCATION} --nanoDriverPort=13923 --nanoDriverClass=it.unibo.refolding.pico.RemoteNanoDriver --nanoDriverDelayMillis=${NANO_DRIVER_DELAY_MILLIS} --nanoDriverSleeper=${NANO_DRIVER_SLEEPER} --pushNm=${PUSH_NM} --waitWhileEmptyMillis=10000 --sleepMillis=25 --forceRecord=false --debug=true --debugPattern=${DEBUG_PATTERN} --minPeakForcePn=${MIN_PEAK_FORCE_PN} --minPeakForceRelativeToEmpiricalBaselinePn=${MIN_PEAK_FORCE_RELATIVE_TO_EMPIRICAL_BASELINE_PN} --medianAbsoluteDeviationLowerBoundPn=${MEDIAN_ABSOLUTE_DEVIATION_LOWER_BOUND_PN} --minPeakContourLengthNm=${MIN_PEAK_CONTOUR_LENGTH} --leftMostlyHigherWindowSizeNm=${LEFT_MOSTLY_HIGHER_WINDOW_SIZE_NM} --leftMostlyHigherFraction=${LEFT_MOSTLY_HIGHER_FRACTION} --leftMostlyHigherPn=${LEFT_MOSTLY_HIGHER_PN} --rightMuchHigherThanAllPn=${RIGHT_MUCH_HIGHER_THAN_ALL_PN} --rightMuchHigherPn=${RIGHT_MUCH_HIGHER_PN} --atMostOnePeakWindowNm=${AT_MOST_ONE_PEAK_WINDOW_NM} --linkerNm=0 --linkerPerModuleNm=4 --maxRequiredWellSpacedAtEnd=2 --clRangeStartNm=${CL_START_NM} --clRangeEndNm=${CL_END_NM} --fetchRefoldBreakParams=Fetch${FETCH_NM}nm_FetchSurfaceDelay${FETCH_SURFACE_DELAY_MS}ms_Refolding${REFOLDING_NM}nm_RefoldingApproachVelocity${REFOLDING_APPROACH_VELOCITY_NM_PER_S}nmPerS_Hover${HOVER_MILLIS}ms_Break${BREAK_NM}nm_Velocity${VELOCITY_NM_PER_S}nmPerS --captureDirectory=${s} --targetDirectory=${d} --svgDir=${p}/analysis/${b}_analysis/${b}_svg --infoDir=${p}/analysis/${b}_analysis/${b}_info --junctionPointsDir=${p}/analysis/${b}_analysis/${b}_jp --contactPointAndSlopeDir=${p}/analysis/${b}_analysis/${b}_cps --peakSlopesDir=${p}/analysis/${b}_analysis/${b}_psi --initialStartRampNm=0 --svgDefScale=${SVG_DEF_SCALE} --minContourLengthDeltaNm=${MIN_CONTOUR_LENGTH_DELTA_NM} --temperatureK=${TEMPERATURE} --persistenceLengthNm=0.35 --asyncSvg=true --defLsbVsZLsbSlope=${SLOPE} --runScriptExtraDelayMillis=500 --allowedTimeRelativeError=${ALLOWED_TIME_RELATIVE_ERROR}); done
